package com.android.publicModule.webview.handler;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class HandlerManage {

	private Context mContext;
	private Activity mActivity;
	private Handler mHandler;
	private Toast toast;
	public static final int SHOW_TOAST = 1;
	
	public HandlerManage(Context context) {
		mContext = context.getApplicationContext();
	}
	
	public void initHandler() {
		if(mHandler==null)
		{
			mHandler = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					if (msg != null){
						switch(msg.what){
							case SHOW_TOAST:
								Bundle bundle = msg.getData();
								if(bundle!=null)
								{
									String text = bundle.getString("text");
									int time = bundle.getInt("time",Toast.LENGTH_SHORT);
									if(text!=null&&text.length()>0)
									{
										if(toast==null)
										{
											toast = Toast.makeText(mContext, text, time);
										}
										else
										{
											toast.setText(text);
											toast.setDuration(time);
										}
										toast.show();
									}
								}
								break;
							case 2:
								break;
							default :
								break;
						}
					}
				}
			};
		}
	}
	
	public void setActivity(Activity activity) {
		if(activity!=null)
		{
			mActivity = activity;
			mContext = activity.getApplicationContext();
			toast = null;
		}
	}
	
	/**
	 * 显示TOAST文本提示信息
	 * @param text
	 */
	public void showToast(String text,int time) {
		if(text!=null&&text.length()>0)
		{
			if(mHandler!=null)
			{
				Message msg = new Message();
				msg.what = SHOW_TOAST;
				Bundle data = new Bundle();
				data.putString("text", text);
				if(time<0)
				{
					time = Toast.LENGTH_SHORT;
				}
				data.putInt("time", time);
				msg.setData(data);
				mHandler.sendMessage(msg);
			}
		}
	}
	
}
